﻿using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApp1
{
    /// <summary>
    /// WindowInit.xaml 的交互逻辑
    /// </summary>
    public partial class WindowInit : Window
    {
        public WindowInit()
        {
            InitializeComponent();
        }
        public  string lang { get; set; }
        SQLiteHelper SQ = new SQLiteHelper();
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            loadDefault();
        }


        private void loadDefault() {


            DataTable data3 = new DataTable();
            data3.Columns.Add("name");
            data3.Columns.Add("value");

            DataRow r3=data3.NewRow();
            r3["name"] = "ug";
            r3["value"] = "ئۇيغۇرچە";
            data3.Rows.Add(r3);

            r3 = data3.NewRow();
            r3["name"] = "cn";
            r3["value"] = "汉语";
            data3.Rows.Add(r3);

            for(int i = 0; i < data3.Rows.Count;i++)
            {
                ComboBoxItem item = new ComboBoxItem();
                item.Content = data3.Rows[i]["value"].ToString();
                item.Tag = data3.Rows[i]["name"].ToString();
                language_combo.Items.Add(item);
            }

            DataTable data = SQ.getData("select * from sys_config where lang='" + lang + "'");
            for (int i = 0; i < data.Rows.Count; i++)
            {
                string name = data.Rows[i]["name"].ToString();
                string value = data.Rows[i]["value"].ToString();
                switch (name)
                {
                    case "resturant_name":
                        txt_resturant_name_ug.Text = value;
                        break;
                   
                    case "tag":
                        txt_resturant_tag.Text = value;
                        break;
                    case "address":
                        txt_resturant_address_ug.Text = value;
                        break;
                   
                    case "tel":
                        txt_resturant_tel.Text = value;
                        break;
                    case "open_time":
                        txt_resturant_opentime.Text = value;
                        break;
                    case "close_time":
                        txt_resturant_closetime.Text = value;
                        break;
                    case "restuarant_id":
                        txt_resturant_id.Text = value;
                        break;
                    case "restuarant_key":
                        txt_resturant_secret.Password = value;
                        break;
                    case "api_token":
                        txt_apitoken.Text = value;
                        break;
                    case "state":

                        break;
                    case "order_interval":
                        txt_order_interval.Text = value;
                        break;
                    case "lang":
                       if (value.Equals("ug"))
                       {
                            //setlanguage(true);
                            language_combo.SelectedIndex = 0;
                       }
                       else if(value.Equals("cn"))
                       {
                            //setlanguage(false);
                            language_combo.SelectedIndex = 1;
                        }


                        break;
                    case "is_dark":
                        if (value.Equals("1"))
                        {
                            toggle_light.IsChecked = true;
                        }
                        else
                        {
                            toggle_light.IsChecked = false;
                        }
                        break;
                    case "primary_color":
                        primarycolor_combo.SelectedValue = value;
                        break;
                    case "paper_width":
                        txt_paper_width.Text = value;
                        break;
                    case "paper_height":
                        txt_paper_height.Text = value;
                        break;
                    case "platform_name":
                        txt_platform_name.Text = value;
                        break;
                }

            }

            DataTable data2 = new DataTable();
            data2.Columns.Add("name");
            data2.Columns.Add("value");

           

            string order_status = "";
            string food_type = "";

             

            if (lang.Equals("ug"))
            {
                 order_status = "زاكاس ھالىتى";
                 food_type = "تاماق تۈرى";
                 


            }
            else
            {
                 order_status = "订单状态";
                 food_type = "饭菜类型";
            

            }

            DataRow r = data2.NewRow();
            r["name"] = "order_status";
            r["value"] = order_status;
            data2.Rows.Add(r);

            r = data2.NewRow();
            r["name"] = "food_type";
            r["value"] = food_type;
            data2.Rows.Add(r);

            data_type_combo.Items.Clear();

            for (int i = 0; i < data2.Rows.Count; i++)
            {

                string nm=data2.Rows[i]["name"].ToString();
                string vl = data2.Rows[i]["value"].ToString();
                ComboBoxItem item = new ComboBoxItem();
                item.Content = vl;
                item.Tag = nm;
                data_type_combo.Items.Add(item);

            }



        }

        string default_name = "";
        private void ToggleButton_Checked(object sender, RoutedEventArgs e)
        {
               ToggleButton tbtn = (ToggleButton)sender;
            string name = tbtn.Name;
            switch (name)
            {
                case "toggle_lang":

                    loadDefault();

                    bool isUg=toggle_lang.IsChecked.Value;

                    setlanguage(isUg);

                   

                    break;
                case "toggle_light":
                    bool flag=toggle_lang.IsChecked.Value;
                    new PaletteHelper().SetLightDark(flag);
                    Palette p = new PaletteHelper().QueryPalette();
                    default_name = p.PrimarySwatch.Name;
                    break;

            }



            // MessageBox.Show(default_name);
        }

        
        private void setlanguage(bool isUg)
        {

            if (isUg)
            {
                lang = "ug";
                this.FlowDirection = FlowDirection.RightToLeft;
                
                lb_lang.Text = "تىل تەڭشىكى";
                lb_theme.Text = "ئۇسلۇب تەڭشىكى";
                lb_light.Text = "يورۇق";
                lb_dark.Text = "قاراڭغۇ";
                HintAssist.SetHint(primarycolor_combo, "ئاساسىي رەڭ");
                
                lb_params.Text = "پارامېتېر تەڭشىكى";

                HintAssist.SetHint(language_combo, "سىستېما تىلى");

                HintAssist.SetHint(txt_resturant_name_ug, "ئاشخانا ئىسمى");
                HintAssist.SetHint(txt_resturant_tag, "چۈشەندۈرۈش");
                HintAssist.SetHint(txt_resturant_tel, "تېلىفون");
                HintAssist.SetHint(txt_resturant_address_ug, "ئادرىسى");

                HintAssist.SetHint(txt_resturant_opentime, "ئېچىلغان ۋاقتى");
                HintAssist.SetHint(txt_resturant_closetime, "توختىتىلىدىغان ۋاقتى");
                HintAssist.SetHint(txt_resturant_id, "ئاشخاناID");
                HintAssist.SetHint(txt_resturant_secret, "ئاشخانا پارولى");

                btn_order_status.Content = "زاكاس ھالىتى تەڭشەش";
                btn_food_type.Content = "تاماق تۈرى تەڭشەش";


                HintAssist.SetHint(txt_status, "ھالىتى");
                HintAssist.SetHint(txt_order_interval, "توردىن زاكاز يېڭىلاش ۋاقتى (سېكۇنت)");
                HintAssist.SetHint(txt_platform_name, "تاماق توشۇش سۇپىسى ئىسمى");

                HintAssist.SetHint(txt_paper_width, "قەغەز كەڭلىكى");
                HintAssist.SetHint(txt_paper_height, "قەغەز ئېگىزلىكى");

                params_btn_sync.Content = "يېڭىلاش";
                params_btn_save.Content = "ساقلاش";
                params_btn_clear.Content = "تازىلاش";


                HintAssist.SetHint(data_type_combo, "ئاساسىي ئۇچۇرلارنى ئۆزگەرتىش");


            }
            else
            {
                lang = "cn";
                this.FlowDirection = FlowDirection.LeftToRight;

                 

                lb_lang.Text = "语言设置";
                lb_theme.Text = "主题设置";
                lb_light.Text = "光亮";
                lb_dark.Text = "暗黑";
                HintAssist.SetHint(primarycolor_combo, "主要颜色");

                HintAssist.SetHint(language_combo, "系统语言");

                lb_params.Text = "参数设置";

                HintAssist.SetHint(txt_resturant_name_ug, "饭店名称");
                HintAssist.SetHint(txt_resturant_tag, "注释");
                HintAssist.SetHint(txt_resturant_tel, "电话");
                HintAssist.SetHint(txt_resturant_address_ug, "地址");

                HintAssist.SetHint(txt_resturant_opentime, "开业时间");
                HintAssist.SetHint(txt_resturant_closetime, "停业时间");
                HintAssist.SetHint(txt_resturant_id, "饭店ID");
                HintAssist.SetHint(txt_resturant_secret, "饭店密码");

                btn_order_status.Content = "订单状态设置";
                btn_food_type.Content = "饭菜类型设置";


                HintAssist.SetHint(txt_status, "状态");
                HintAssist.SetHint(txt_order_interval, "订单读取间隔(秒)");
                HintAssist.SetHint(txt_platform_name, "外卖平台名称");

                HintAssist.SetHint(txt_paper_width, "纸张宽度");
                HintAssist.SetHint(txt_paper_height, "纸张长度");

                params_btn_sync.Content = "同步";
                params_btn_save.Content = "保存";
                params_btn_clear.Content = "清空";


                HintAssist.SetHint(data_type_combo, "静态数据编辑");



            }


        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            ComboBox combo = (ComboBox)sender;

            string name=combo.Name;

            switch (name)
            {
                case "primarycolor_combo":

                    ComboBoxItem item = (ComboBoxItem)primarycolor_combo.SelectedItem;


                    if (item != null)
                    {
                        string color = item.Content.ToString();

                        if (color.Equals("Default"))
                        {

                            color = default_name;

                            return;

                        }

                        new PaletteHelper().ReplacePrimaryColor(color);


                    }



                    break;
                case "data_type_combo":

                     item = (ComboBoxItem)data_type_combo.SelectedItem;
                    if (item != null)
                    {
                        string type = item.Tag.ToString();

                        StaticDataEditWindow staticDataEditWindow = new StaticDataEditWindow();
                        staticDataEditWindow.init(type, lang);
                        staticDataEditWindow.ShowDialog();
                    }

                    break;
                case "language_combo":
                    int index=language_combo.SelectedIndex;
                    switch (index)
                    {
                        case 0:
                            setlanguage(true);
                            break;
                        case 1:
                            setlanguage(false);
                            break;
                    }

                    break;
            }

           

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)sender;
            string name = btn.Name;
            switch (name)
            {
                case "params_btn_save":

                    save();

                    break;
                case "btn_food_type":

                    
                        string type = "food_type";

                        StaticDataEditWindow staticDataEditWindow = new StaticDataEditWindow();
                        staticDataEditWindow.Owner = this;
                        staticDataEditWindow.init(type, lang);
                        staticDataEditWindow.ShowDialog();
                    


                    break;
                case "btn_order_status":
                    type = "order_status";

                    staticDataEditWindow = new StaticDataEditWindow();
                    staticDataEditWindow.Owner = this;
                    staticDataEditWindow.init(type, lang);
                    staticDataEditWindow.ShowDialog();

                    break;
                case "params_btn_sync":

                    string restuarant_id = txt_resturant_id.Text;
                    string restuarant_key = txt_resturant_secret.Password;
                    if (string.IsNullOrEmpty(restuarant_id) || string.IsNullOrEmpty(restuarant_key))
                    {
                        return;
                    }
                  bool flag=service.syncReturant(restuarant_id, restuarant_key, lang);
                    if (!flag)
                    {
                        string msg = "";
                        if(lang.Equals("ug"))
                        {
                            msg = "يېڭىلاش مەغلۇب بولدى";
                        }
                        else
                        {
                            msg = "同步失败";
                        }
                        MessageBox.Show(msg);
                        return;
                    }
                    loadDefault();
                    break;



            }
        }


        private void save()
        {


            
            string  name_ug= txt_resturant_name_ug.Text;
            string tag=txt_resturant_tag.Text;
            string address_ug=txt_resturant_address_ug.Text;
            string tel1=txt_resturant_tel.Text;
            string open_time=txt_resturant_opentime.Text;
            string close_time=txt_resturant_closetime.Text;
            string restuarant_id=txt_resturant_id.Text;
            string restuarant_key=txt_resturant_secret.Password;
            string api_token=txt_apitoken.Text;
            string order_interval = txt_order_interval.Text;
            string platform_name = txt_platform_name.Text;

            string paper_width = txt_paper_width.Text;
            string paper_height = txt_paper_height.Text;

            string lang = "";

            string is_dark = "0";

            if (toggle_light.IsChecked.Value)
            {
                is_dark = "1";
            }
            else
            {
                is_dark = "0";
            }

            if (language_combo.SelectedItem==null)
            {
                return;
            }

            string primary_color = "Teal";

            if (primarycolor_combo.SelectedItem != null)
            {
                ComboBoxItem item=(ComboBoxItem)primarycolor_combo.SelectedItem;
                primary_color = item.Content.ToString();
            }

            int index=language_combo.SelectedIndex;

            if (index==0)
            {
                lang = "ug";

                if (string.IsNullOrEmpty(name_ug))
                {
                    MessageBox.Show("ئاشخانا ئىسمىنى تولدۇرۇڭ");
                    return;
                }


                if (string.IsNullOrEmpty(order_interval))
                {
                    MessageBox.Show("توردىن زاكاز يېڭىلاش ئارىلىقىنى تولدۇرۇڭ");
                    return;
                }
            

                

                if (string.IsNullOrEmpty(restuarant_id))
                {
                    MessageBox.Show("ئاشخانا ID  نومۇرىنى كىرگۈزۈڭ");
                    return;
                }
                if (string.IsNullOrEmpty(restuarant_key))
                {
                    MessageBox.Show("ئاشىانا پارولىنى كىرگۈزۈڭ");
                    return;
                }

            }
            else
            {
                lang = "cn";
                if (string.IsNullOrEmpty(name_ug))
                {

                    MessageBox.Show("请输入饭店名称");
                    
                    return;

                }

                if (string.IsNullOrEmpty(order_interval))
                {
                    MessageBox.Show("请输入订单读取间隔(s)");
                    return;

                }


                if (string.IsNullOrEmpty(restuarant_id))
                {

                    MessageBox.Show("请输入 饭店ID");
                    return;
                }

                if (string.IsNullOrEmpty(restuarant_key))
                {

                    MessageBox.Show("请输入 饭店密码");
                    return;
                }



            }


            bool flag=service.test(restuarant_id, restuarant_key);

            if (!flag)
            {
                string msg1 = "";
                if (lang.Equals("ug"))
                {
                    msg1 = "توردىن ئۇچۇر ئېلىش مەغلۇپ بولدى";
                }
                else
                {
                    msg1 = "网络数据获取失败";
                }

                MessageBox.Show(msg1);

                return;
            }
            string sql = "";

            sql = "update sys_config set value='" + name_ug + "',lang='"+lang+"' where name='resturant_name';";
            //SQ.update(sql);
           
            sql+= "update sys_config set value='" + tag + "',lang='" + lang + "' where name='tag';";
            //SQ.update(sql);
            sql+= "update sys_config set value='" + address_ug + "',lang='" + lang + "' where name='address';";
            //SQ.update(sql);
           
            sql+= "update sys_config set value='" + tel1 + "',lang='" + lang + "' where name='tel';";
            //SQ.update(sql);

            sql+= "update sys_config set value='" + open_time + "',lang='" + lang + "' where name='open_time';";
            //SQ.update(sql);

            sql+= "update sys_config set value='" + close_time + "',lang='" + lang + "' where name='close_time';";
            //SQ.update(sql);

            sql+= "update sys_config set value='" + restuarant_id + "',lang='" + lang + "' where name='restuarant_id';";
            //SQ.update(sql);

            sql+= "update sys_config set value='" + restuarant_key + "',lang='" + lang + "' where name='restuarant_key';";
            //SQ.update(sql);

            sql+= "update sys_config set value='" + api_token + "',lang='" + lang + "' where name='api_token';";
            //SQ.update(sql);

            // sql = "update sys_config set value='" + state + "' where name='state';";
            sql+= "update sys_config set value='" + lang + "',lang='" + lang + "' where name='lang';";
            //SQ.update(sql);

            sql+= "update sys_config set value='" + order_interval + "',lang='" + lang + "' where name='order_interval';";
            //SQ.update(sql);

            sql+= "update sys_config set value='" + platform_name + "',lang='" + lang + "' where name='platform_name';";
            //SQ.update(sql);

            sql+= "update sys_config set value='" + is_dark + "',lang='" + lang + "' where name='is_dark';";
            //SQ.update(sql);


            sql+= "update sys_config set value='" + primary_color + "',lang='" + lang + "' where name='primary_color';";

            sql += "update sys_config set value='" + paper_width + "',lang='" + lang + "' where name='paper_width';";

            sql += "update sys_config set value='" + paper_height + "',lang='" + lang + "' where name='paper_height';";
            
            SQ.update(sql);
            
            string msg = "";
            if (lang.Equals("ug"))
            {
                msg = "ئوڭۇشلۇق بولدى";
            }
            else
            {
                msg = "操作成功";
            }

            MessageBox.Show(msg);
            
        }

        Service service = new Service();

        private void Window_Closed(object sender, EventArgs e)
        {

            
                MainWindow mainWindow = new MainWindow();
                mainWindow.Show();
            
        }
    }
}
